/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacen;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author Tesla
 */
public class almacenado extends javax.swing.JFrame {

    Connection cn;
    PreparedStatement ps;
    ResultSet rs;
    static int code;
    int id_proveedor;

    /**
     * Creates new form almacenado
     */
    public almacenado(int cd) {
        initComponents();
        code = cd;
        try {
            cn = local_connection.getconnect();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        iniciarValores();
    }

    void iniciarValores() {
        try {
            ps = (PreparedStatement) cn.prepareStatement("select minimo_stock,cantidad_stock,codigo_ubicacion,cantidadalmacen,ubicacionalmacen,id_proveedor from productos where id_producto = " + code);
            rs = ps.executeQuery();
            rs.next();
            //System.out.println("select minimo_stock,cantidad_stock,codigo_ubicacion,cantidadalmacen,ubicacionalmacen,id_proveedor from productos where id_producto = " + code);
            minimo_stock.setText(rs.getString("minimo_stock"));
            codigo_ubicacion.setText(rs.getString("codigo_ubicacion"));
            cantidad_stock.setText(rs.getString("cantidad_stock"));
            ubicacionalmacen.setText(rs.getString("ubicacionalmacen"));
            cantidadalmacen.setText(rs.getString("cantidadalmacen"));
            stocktotal.setText(String.valueOf(rs.getInt("cantidadalmacen") + rs.getInt("cantidad_stock")));
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

    }
    private void insertarRevision(){
            try {
                ps = (PreparedStatement) cn.prepareStatement("SELECT * FROM revisiones WHERE idProducto = " + code);
                rs = ps.executeQuery();
                int id = (int) code;
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                Date date = (Date) new Date();
                int almacen = (int) Integer.parseInt(cantidadalmacen.getText());
                int stock = (int) Integer.parseInt(cantidad_stock.getText());
                if(rs.isBeforeFirst()){
                    ps = (PreparedStatement) cn.prepareStatement("UPDATE revisiones SET fecha = ? WHERE idProducto = "+id);
                    ps.setString(1,dateFormat.format(date));
                } else {
                    date = new Date();
                    ps = (PreparedStatement) cn.prepareStatement("INSERT INTO revisiones(idProducto,cantidadalmacen,cantidad_stock,fecha) VALUES(?,?,?,?)");
                    ps.setInt(1,id);
                    ps.setInt(2,almacen);
                    ps.setInt(3,stock);
                    ps.setString(4,dateFormat.format(date));
                }                
                ps.execute();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cantidad_stock = new javax.swing.JTextField();
        codigo_ubicacion = new javax.swing.JTextField();
        cantidadalmacen = new javax.swing.JTextField();
        ubicacionalmacen = new javax.swing.JTextField();
        stocktotal = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        minimo_stock = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getResource("logo_rayito.png")).getImage());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Stock Total:");

        jLabel2.setText("Cantidad en mostrador:");

        jLabel3.setText("Cantidad en almacen:");

        jLabel4.setText("Ubicacion en mostrador:");

        jLabel5.setText("Ubicacion en almacen:");

        stocktotal.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        stocktotal.setText("----");

        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Proveedor");

        jLabel7.setText("Minimo en stock:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel5))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cantidadalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(ubicacionalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cantidad_stock, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(codigo_ubicacion, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(jButton1)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(jLabel1)
                                .addGap(38, 38, 38)
                                .addComponent(stocktotal))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addGap(55, 55, 55)
                        .addComponent(minimo_stock)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(stocktotal, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(minimo_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(29, 29, 29))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cantidad_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(codigo_ubicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cantidadalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ubicacionalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            ps = (PreparedStatement) cn.prepareStatement("Update productos set minimo_stock = ?, cantidad_stock = ?, codigo_ubicacion = ?, cantidadalmacen = ?, ubicacionalmacen = ? where id_producto = " + code);
            ps.setInt(1, Integer.parseInt(minimo_stock.getText()));
            ps.setInt(2, Integer.parseInt(cantidad_stock.getText()));
            ps.setString(3, codigo_ubicacion.getText());
            ps.setInt(4, Integer.parseInt(cantidadalmacen.getText()));
            ps.setString(5, ubicacionalmacen.getText());
            ps.execute();
            insertarRevision();
            this.dispose();
            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(almacenado.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cantidad_stock;
    private javax.swing.JTextField cantidadalmacen;
    private javax.swing.JTextField codigo_ubicacion;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField minimo_stock;
    private javax.swing.JLabel stocktotal;
    private javax.swing.JTextField ubicacionalmacen;
    // End of variables declaration//GEN-END:variables
}
